<template>
  <div class="page"></div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router'
import { useStore } from '@/store/index'
import api from 'js/api'
import md5 from 'js-md5'

const router = useRouter()
const route = useRoute()
const store = useStore()

login()

// 登录
async function login() {
  if (store.userInfo?.token) {
    // 已登录
    return router.replace('/')
  }

  const wallet = fromAddress
  const timestamp = Date.now() / 1000

  const params = {
    wallet,
    timestamp,
    sign: md5(wallet + timestamp),
  }

  const { code, data } = await api.login(params)

  if (code === 1) {
    store.setUserInfo(data.userinfo)

    if (data.userinfo?.parent_id) {
      // 已注册
      router.replace('/')
    } else {
      // 未注册
      const inviteId = route.query.inviteId || ''
      router.replace(`/register?inviteId=${inviteId}`)
    }
  }
}
</script>

<style lang="scss" scoped></style>
